package com.load.utils;

import java.io.*;
import java.net.*;
import java.util.*;
import java.util.regex.*;

import org.apache.commons.io.IOUtils;


public class IssueUtil {

	private static final int OFFSET = 25;

	@SuppressWarnings("unchecked")
	public static List<Map<KeyIssue, String>> formatGoogleCodeDistribution(InputStream input) throws Exception {
		List<Map<KeyIssue, String>> issues = new ArrayList<Map<KeyIssue, String>>();
		Iterator<String> lines = IOUtils.readLines(input).iterator();
		lines.next();
		while (lines.hasNext()) {
			String line = lines.next();
			line = line.trim();

			if (line.length() > 0) {
				Map<KeyIssue, String> issue = new HashMap<KeyIssue, String>();
				String[] tokens = splitExtended(line, ",");
				issue.put(KeyIssue.NUMBER, tokens[0]);
				issue.put(KeyIssue.TYPE, tokens[1]);
				issue.put(KeyIssue.STATUS, tokens[2]);
				issue.put(KeyIssue.PRIORITY, tokens[3]);
				issue.put(KeyIssue.DEVELOPER, tokens[5]);
				issue.put(KeyIssue.DESCRIPTION, tokens[6]);
				issues.add(issue);
			}
		}
		return issues;
	}

	private static String[] splitExtended(String line, String regex) {
		String[] tokens = line.split(regex);
		String[] processedTokens = new String[tokens.length];

		for (int i = 0; i < tokens.length; i++) {
			String token = tokens[i];
			token = token.trim();
			token = token.replace("\"", "");
			processedTokens[i] = token;
		}
		return processedTokens;
	}

	public static List<Map<KeyIssue, String>> formatSourceForgeDistribution(String url, Pattern issuePattern, Pattern developerPattern) throws MalformedURLException, IOException {
		List<Map<KeyIssue, String>> issues = new ArrayList<Map<KeyIssue, String>>();

		int i = 0;
		String page = obtainPage(url, i);
		boolean pageHasElements = true;
		while (pageHasElements) {
			Matcher matcherIssue = issuePattern.matcher(page);

			int counter = 0;
			while (matcherIssue.find()) {
				Map<KeyIssue, String> issue = new HashMap<KeyIssue, String>();
				issue.put(KeyIssue.PRIORITY, matcherIssue.group(1));
				issue.put(KeyIssue.NUMBER, matcherIssue.group(2));
				issue.put(KeyIssue.TYPE, matcherIssue.group(4));
				issue.put(KeyIssue.DESCRIPTION, matcherIssue.group(7));
				issue.put(KeyIssue.DEVELOPER, obtainDeveloper(matcherIssue.group(8), developerPattern));
				issue.put(KeyIssue.STATUS, matcherIssue.group(12));
				issue.put(KeyIssue.DATE, matcherIssue.group(13));
				issues.add(issue);
				counter++;
			}
			if (counter == 0) {
				pageHasElements = false;
			} else {
				i += OFFSET;
				page = obtainPage(url, i);
			}
		}
		return issues;
	}

	private static String obtainPage(String url, int index) throws MalformedURLException, IOException {
		url = url.replace(":offset", String.valueOf(index));
		InputStream inputStream = new URL(url).openStream();
		return readPage(inputStream);
	}

	@SuppressWarnings("unchecked")
	private static String readPage(InputStream inputStream) throws IOException {
		List<String> readLines = IOUtils.readLines(inputStream);
		StringBuilder builder = new StringBuilder();
		for (String line : readLines) {
			builder.append(line);
		}
		String cleanedLines = builder.toString().replaceAll("\n", "").replaceAll("\\s+", " ");
		return cleanedLines;
	}

	private static String obtainDeveloper(String text, Pattern developerPattern) {
		Matcher matcherDev = developerPattern.matcher(text);
		String developer = text;
		if (matcherDev.find()) {
			developer = matcherDev.group(1);
		}
		return developer;
	}

}